package com.nstar.orderexpress.view
{
	import com.adobe.ac.mxeffects.DistortionConstants;
	import com.nstar.base.util.EffectUtils;
	import com.nstar.orderexpress.control.OrderMenuListEvent;
	import com.nstar.orderexpress.control.OrderMenuThumbEvent;
	import com.nstar.orderexpress.model.Model;
	import com.nstar.orderexpress.mxml.OrderMenuList;
	import com.nstar.orderexpress.vo.OrderMenuVO;
	
	import mx.containers.Canvas;
	import mx.events.FlexEvent;
	
	public class FavoriteViewClass extends Canvas
	{
		public var omlFavorites:OrderMenuList;
		public var omlOrderHis:OrderMenuList;
		
		private var model:Model = Model.getInstance();
		
        [Bindable]
        public var numOrderMenus:int=0;			
		
		public function FavoriteViewClass()
		{
			//TODO: implement function
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.addEventListener(OrderMenuThumbEvent.ADD_FAVORITE,addFavoriteHandler);
		}
		
		private function init(event:FlexEvent):void{
			omlFavorites.addEventListener(OrderMenuListEvent.ADD_MENU,orderMenuListHandler);
			omlFavorites.addEventListener(OrderMenuListEvent.DUPLICATE_MENU,orderMenuListHandler);
			omlFavorites.addEventListener(OrderMenuListEvent.MENU_QTY_CHANGE,orderMenuListHandler);
			omlFavorites.addEventListener(OrderMenuListEvent.REMOVE_MENU,orderMenuListHandler);			
		}
		
		private function addFavoriteHandler(event:OrderMenuThumbEvent):void{
			if (model.mainApp.vsControl.selectedIndex != 2){
				/* var target:Object = model.mainApp.vsControl.getChildAt(model.mainApp.tbbControl.selectedIndex);
				var siblings:Object = model.mainApp.favoriteView;
				EffectUtils.rotateView(DistortionConstants.LEFT,target,[siblings],500); */
				model.mainApp.tbbControl.selectedIndex = 2;
			}
		}
		
		//菜单事件处理
		private function orderMenuListHandler(event:OrderMenuListEvent):void{
            switch (event.type)
            {
                case OrderMenuListEvent.ADD_MENU:
                    event.orderMenu.quantity = 0;  
                    //fall through into the same logic as dup          
                case OrderMenuListEvent.DUPLICATE_MENU:
                    event.orderMenu.quantity++;
                    numOrderMenus++;
                    break;            
                case OrderMenuListEvent.MENU_QTY_CHANGE:
                case OrderMenuListEvent.REMOVE_MENU:
                    var items:Array = omlFavorites.items;
                    numOrderMenus = 0;
                    for (var i:int=0; i < items.length; i++)
                    {
                        var orderMenu:OrderMenuVO = items[i].orderMenu;
                        numOrderMenus += orderMenu.quantity;
                    }    
                    addFavoriteHandler(null);   
                    break;
                default:
                    break;
            }			
		}
	}
}